package com.wanxi.controller;

import com.wanxi.model.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManageSystem {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("-----欢迎来到学生管理系统-----");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择:");
            int type = scanner.nextInt();
            switch (type) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    updateStudent(list);
                    break;
                case 4:
                    findAllStudent(list);
                    break;
                case 5:
                    return;
                default:
                    break;
            }
        }
    }


    private static void findAllStudent(ArrayList<Student> list) {
        System.out.println("学号         姓名          年龄       生日");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getSid() +
                    "       " + list.get(i).getName() +
                    "       " + list.get(i).getAge() +
                    "岁       " + list.get(i).getBirthday());
        }
        System.out.println("查询所有");
    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要修改的学生id");
        String sid = scanner.next();
        if (!judge(sid, list)) {
            System.out.println("你输入的sid不存在,修改失败");
            return;
        }
        System.out.println("请输入需要修改的学生信息");

        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        String age = scanner.next();
        System.out.println("请输入学生生日");
        String birthday = scanner.next();
        Student student = new Student(sid, name, birthday, age);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                list.set(i, student);
                break;
            }
        }
        System.out.println("修改学生成功");
    }

    /**
     *
     * @param list
     */
    private static void deleteStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要删除的学生id");
        String sid = scanner.next();
//        1.首先需要判断当前的id是否在集合里面存在，
        if (!judge(sid, list)) {
            System.out.println("你输入的sid不存在,删除失败");
            return;
        }
//        2.如果不存在，则提示删除失败，不存在当前学生
//        3.如果存在则提示成功
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                list.remove(list.get(i));
                break;
            }
        }
        System.out.println("删除成功");
    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要添加的学生信息");
        System.out.println("请输入学生学号");
        String sid = scanner.next();
        if (judge(sid, list)) {
            System.out.println("当前输入的sid已经存在，不能重复添加");
            return;
        }
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        String age = scanner.next();
        System.out.println("请输入学生生日");
        String birthday = scanner.next();
        Student student = new Student(sid, name, birthday, age);
        list.add(student);
        System.out.println("增加成功");

    }

    private static boolean judge(String sid, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                return true;
            }
        }
        return false;
    }
}
